package com.xjh.utils.pageutils;

import java.io.Serializable;
import java.util.List;

public class IPage<T> implements Serializable {
    /**
     * 默认10条
     */
    private static int default_page_size = 10;

    /**
     * 每页条数
     */
    private Integer pageSize;

    /**
     * 当前行索引位置
     */
    private Integer offset = 0;

    /**
     * 当前页码
     */
    private Integer pageNum = 1;

    /**
     * 总页数
     */
    private Integer totalPage = 1;

    /**
     * 总条数
     */
    private long totalSize = 0;

    /**
     * 结果集
     */
    private List<T> result;

    public IPage() {

        this.pageNum = 1;
        this.pageSize = default_page_size;

    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = (null == pageSize) ? default_page_size : pageSize;
    }

    public Integer getOffset() {
        if (null == pageNum || pageNum < 1) {
            pageNum = 1;
        } else if (getTotalPage() > 0 && pageNum > getTotalPage()) {
            pageNum = getTotalPage();
        }

        offset = (pageNum - 1) * pageSize;

        return offset;
    }

    public void setOffset(Integer offset) {
        this.offset = offset;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = (null == pageNum) ? 1 : pageNum;
    }

    public Integer getTotalPage() {
        totalPage = new Long(totalSize).intValue() / pageSize;
        if (totalSize % pageSize != 0) {
            totalPage++;
        }
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public long getTotalSize() {
        return totalSize;
    }

    public void setTotalSize(long totalSize) {
        this.totalSize = totalSize;
    }

    public List<T> getResult() {
        return result;
    }

    public void setResult(List<T> result) {
        this.result = result;
    }

}
